#include "TcpServer.hpp"
#include "http.hpp"

void Usage()
{
    std::cout << "Usage:\r\n";
    std::cout << "./tcpserver port\n";
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    std::string user_data;
    // 根据提交方法获取用户提交的参数
    if (strcasecmp(req->Method().c_str(), "get") == 0)
    {
        user_data = req->Args();
    }
    else if (strcasecmp(req->Method().c_str(), "post") == 0)
    {
        user_data = req->Text();
    }
    else {}

    // 1.进程间通信 pipe

    // 2.fork创建子进程

    // 3.exec程序替换 java/python/c++ 对参数进行处理

    // 此时数据已经处理完毕
    LOG(INFO, "data handle done, data is :%s", user_data.c_str());
    auto response = Factory::BuildHttpResponse();
    response->AddStatusLine(200, "ok");
    response->AddHeadersMap("Content-type", "text/html");
    std::string content = "<html><h1>Hello, Http</h1></html>";
    response->AddContent(content);

    return response;
}
int main(int argn, const char *argv[])
{
    if (argn != 2)
    {
        Usage();
        exit(EXIT_FAILURE);
    }
    uint16_t port = std::stoi(argv[1]);
    HttpServer httpservice;
    httpservice.AddServer("/login", Login);
    TcpServer tcpsvr(port, std::bind(&HttpServer::HttpServerHelper, &httpservice, std::placeholders::_1));
    tcpsvr.Loop();
}